<?php

namespace app\mobile\controller;

use think\Controller;
use app\mobile\model\Order as orderModel;
use think\Request;


class Order extends Controller
{
    //当提交的姓名、医院、网址是相同的，就表示用户在重复提交
    public function dopost(Request $request)
    {
        header('Access-Control-Allow-Origin:*');
        // $params = $request->param();
        $params['name'] = $request->param('name');
        $params['phone'] = $request->param('phone');
        $params['idcard'] = $request->param('idcard');
        $params['check_time'] = $request->param('check_time');
        $params['sex'] = $request->param('sex');
        $params['diabetes'] = $request->param('diabetes');
        $params['hospital'] = $request->param('hospital');
        $params['weburl'] = $request->param('weburl');
        $params['message'] = strlen($request->param('message')) > 100 ? substr($request->param('message'),0,100): $request->param('message');
        $params['ip'] = get_ip();
        $params['city'] = getLocationByIp($params['ip']);
        $params['from'] = 1;
        $params['update_time'] = date('Y-m-d H:i:s');
        $params['addtime'] = date('Y-m-d H:i:s');
        $params['status'] = 1;
		$params['state'] = 1;
//        $callback = $request->param('callback');

        //当提交的姓名、医院、网址是相同的，就表示用户在重复提交
        $where = [
            'name' => $params['name'],
            'hospital' => $params['hospital'],
            'weburl' => $params['weburl']
        ];
        $order = new orderModel();
        $has = $order->where($where)->select(); //当$has存在时 表示数据库已经存在

        //发送邮件
        $totitle = $params['weburl'] . '预约表单_' . date('Y-m-d');
        $tobody = '体检人姓名：' . $params['name'] . '<br>';
        $tobody .= '体检人身份证号：' . $params['idcard'] . '<br>';
        $tobody .= '体检人联系电话：' . $params['phone'] . '<br>';
        if (!empty($params['sex'])) {
            $tobody .= '性别：' . $params['sex'] . '<br>';
        }
        if (!empty($params['diabetes'])) {
            $tobody .= '是否有糖尿病：' . $params['diabetes'] . '<br>';
        }

        $tobody .= '体检日期：' . date('Y-m-d',$params['check_time']) . '<br>';
        $tobody .= '预约医院：' . $params['hospital'] . '<br>';
        if (!empty($params['message'])) {
            $tobody .= '说明：' . $params['message'] . '<br>';
        }
        $tobody .= '表单提交所在地：' . $params['city'] . '<br>';
        $toarremail = config('web.email_address');

        if (!$has) {
            send_mail($toarremail, $toarratt = null, $totitle, $tobody);
            if ($order->insert($params, true)) {
                $json = ["status" => 1, "msg" => "预约成功，客服将会在30分钟之内与您联系!"];
            } else {
                $json = ["status" => 0, "msg" => "预约失败，请重新提交信息"];
            }
        } else {
            $json = ["status" => 0, "msg" => "你已经成功提交过，请不要重复提交！"];
        }

        $jsonp = jsonp($json)->options([
            'var_jsonp_handler' => 'callback',
            'default_jsonp_handler' => 'jsonpReturn',
            'json_encode_param' => JSON_PRETTY_PRINT,
        ]);

        return $jsonp;
    }
}